﻿using System;
using System.Windows;
using System.Windows.Input;
using Microsoft.Windows.Controls.Ribbon;
using System.Windows.Controls;
using E4D.AppFabric.Entities;

namespace MDCAdminTool
{
    public partial class MainWindow : RibbonWindow
    {
        private object _SelectedItem;

        public object SelectedItem
        {
            get { return _SelectedItem; }
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void ClustersTreeView_SelectedItemChanged( object sender , RoutedPropertyChangedEventArgs<object> e )
        {
            _SelectedItem    = e.NewValue;
            DataContext      = _SelectedItem;

            SetRibbonTab();
        }

        private void SetRibbonTab()
        {
            if ( SelectedItem is CacheCluster )
            {
                ribbon.SelectedTab = rtCluster;
            }
            else if ( SelectedItem is CacheHost )
            {
                ribbon.SelectedTab = rtHost;
            }
            else if ( SelectedItem is MdcCache )
            {
                ribbon.SelectedTab = rtCache;
            }
            else if ( SelectedItem is CacheRegion )
            {
                ribbon.SelectedTab = rtCache;
            }
        }

        private void ClustersTreeView_MouseDoubleClick( object sender , MouseButtonEventArgs e )
        {
            App.NewTab();
        }

        internal static void ReLoadClusterTreeView()
        {
            var tv = Application.Current.Properties["ClustersTreeView"] as TreeView;
            var clusters = Application.Current.Properties["Clusters"] as Clusters;

            if (tv != null)
                tv.Dispatcher.Invoke(
                        new Action( () => { tv.DataContext = null;
                                            tv.DataContext = clusters; } )
                    );
        }

        private void RibbonWindow_Loaded( object sender , RoutedEventArgs e )
        {
            App.Tree        = ClustersTreeView;
            App.Output      = lbOutput;
            App.TabControl  = tabControl;
            App.ProgressBar = powerShellProgressBar;

            //systemRequirementsCheck.Visibility = Visibility.Collapsed;
            //systemRequirementsCheck.BeginRun();

            systemRequirementsCheck_EndCheck(this , EventArgs.Empty);
            
        }

        private void EndLoadCacheCluster(IAsyncResult ar)
        {
            var func = ar.AsyncState as Action;
            if( func != null)
                func.EndInvoke(ar);

            ClustersTreeView.Dispatcher.Invoke(
                new Action( 
                    () => 
                        {
                            ClustersTreeView.DataContext =
                                Application.Current.Properties["Clusters"] as Clusters;

                           // ClustersTreeView.SetBinding(TreeView.ItemsSourceProperty, string.Empty);

                            powerShellProgressBar.Stop();
                        } ) );
        }
        private void LoadCacheCluster()
        {
            var clusters = new Clusters {Logger = lbOutput};
            clusters.UseLocalCacheCluster();

            App.Clusters = clusters;
        }

        private void systemRequirementsCheck_EndCheck( object sender , EventArgs e )
        {
            Action func = LoadCacheCluster;
            powerShellProgressBar.Start();
            func.BeginInvoke( EndLoadCacheCluster , func );
        }

        //private void CommandBinding_CanExecute( object sender , CanExecuteRoutedEventArgs e )
        //{
        //    //if ( !systemRequirementsCheck.IsOk && !systemRequirementsCheck.IsRun)
        //        e.CanExecute = true;
        //}
        //private void CommandBinding_Executed( object sender , ExecutedRoutedEventArgs e )
        //{
        //    systemRequirementsCheck.BeginRun();
        //    systemRequirementsCheck.Visibility = Visibility.Visible;
        //}
    }
}
